<template>
    <div>
        <el-form :inline="true" :model="teacher" class="demo-form-inline">
            <el-form-item label="教师编号">
                <el-input placeholder="请输入教师编号" v-model="teacher.teacher_id" clearable></el-input>
            </el-form-item>
            <el-form-item label="教师姓名">
                <el-input placeholder="请输入教师姓名" v-model="teacher.teacher_name" clearable></el-input>
            </el-form-item>
            <br />
            <el-form-item label="教师电话">
                <el-input placeholder="请输入教师电话" v-model="teacher.teacher_phone" clearable></el-input>
            </el-form-item>
            <el-form-item label="教师级别">
                <template>
                    <el-select v-model="teacher.rank_name" placeholder="请选择级别">
                        <el-option v-for="r in rankData" :key="r" :label="r" :value="r"></el-option>
                    </el-select>
                </template>
            </el-form-item>
            <br />
            <el-form-item>
                <el-button type="primary" @click="tcQuery">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="warning" @click="toReset">重置</el-button>
            </el-form-item>
        </el-form>
        <table style="width: 100%; border: none; margin-top: 30px;" cellspacing="0">
            <tr style="font-weight: 800;">
                <td>教师编号</td>
                <td>教师姓名</td>
                <td>教师电话</td>
                <td>教师级别</td>
                <td>
                    <el-button type="info" disabled>操作</el-button>
                    <el-button type="success" @click="stuAdd()">新增</el-button>
                </td>
            </tr>
            <tr v-for="tc in getTeacherMsg" :key="tc.teacher_id">
                <td>{{ tc.teacher_id }}</td>
                <td>{{ tc.teacher_name }}</td>
                <td>{{ tc.teacher_phone }}</td>
                <td>{{ tc.rank_name }}</td>
                <td>
                    <el-button type="primary" @click="teacherQuery(tc.teacher_id)">修改</el-button>
                    <el-button type="danger" @click="delConfirm(tc.teacher_id)">删除</el-button>
                </td>
            </tr>
        </table>
        <router-view></router-view>
    </div>
</template>

<script>
    export default {
        name:"TeacherQuery",
        data() {
            return {
                teacher: {
                    teacher_id:"",
                    teacher_name:"",
                    teacher_phone:"",
                    rank_name:"",
                },
                rankData:[],
                getTeacherMsg: []
            }
        },
        created() {
            this.tcQuery();
            this.getData();
        },
        methods: {
            getData() {
                this.$axios({
                    methods:'get',
                    url:'http://localhost:8081/workbench/teacher/getData',
                }).then(res=>{
                    this.rankData = res.data.data[1];
                }).catch(err=>{
                    console.log(err);
                })
            },
            tcQuery() {
                this.$axios({
                    methods:'get',
                    url:'http://localhost:8081/workbench/teacher/findTeacherPart',
                    params:this.teacher
                }).then(res=>{
                    this.getTeacherMsg=res.data.data;
                }).catch(err=>{
                    console.log(err);
                })
            },
            delConfirm(teacher_id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                this.tcDel(teacher_id);
                this.$message({ 
                    type: 'success',
                    message: '删除成功!'
                });
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
                });
            },
            tcDel(teacher_id) {
                this.$axios({
                    methods:'get',
                    url:'http://localhost:8081/workbench/teacher/removeTeacherById',
                    params: {
                        teacher_id:teacher_id
                    }
                }).then(res=>{
                    this.tcQuery();
                }).catch(err=>{
                    console.log(err);
                })
            },
            toReset() {
                this.teacher.teacher_id="";
                this.teacher.teacher_name="";
                this.teacher.teacher_phone="";
                this.teacher.rank_name="";
                this.tcQuery();
            },
            teacherQuery(teacher_id) {
                this.$router.replace({
                    name:'updateTeacher', 
                    params:{
                        teacher_id:teacher_id,
                    }
                })
            },
            stuAdd() {
                this.$router.replace({
                    name:'addTeacher', 
                })
            }
        }
    }
</script>

<style>
    .el-input {
        margin-right: 30px;
    }
    .el-form-item {
        margin: 0 80px;
    }
    .cell {
        text-align: center !important;
    }
    tr {
        height: 60px;
        text-align: center;
    }
    tr:nth-child(odd) {
        background-color: #fff;
    }
    tr:nth-child(even) {
        background-color: #f2f2f2;
    }
    td {
        text-align: center;
    }
</style>